/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package views;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import models.Client;
import models.ClientDAO;
import models.Order;
import models.OrderDAO;
import models.OrderItem;
import models.Product;
import models.ProductDAO;
import models.User;
import models.UserDAO;

/**
 *
 * @author daniel
 */
public class SaleForm extends javax.swing.JInternalFrame {
    
    ClientDAO cdao = new ClientDAO();
    ProductDAO pdao = new ProductDAO();
    OrderDAO odao = new OrderDAO();
    UserDAO udao = new UserDAO();
    
    User u = new User();
    Client client = new Client();
    Product p = new Product();
    Order o = new Order();
    OrderItem oi = new OrderItem();
    
    DefaultTableModel model = new DefaultTableModel();
    
    int idp;
    double price;
    int qty;
    
    double totalPayment;

    /**
     * Creates new form SaleForm
     */
    public SaleForm() {
        initComponents();
        
        genDate();
        genSerialNum();
        
    }
    
    

    void genDate() {
        Calendar calendar = new GregorianCalendar();
        txtDateOrder.setText(""+calendar.get(Calendar.YEAR)+"-"+calendar.get(Calendar.MONTH)+"-"+calendar.get(Calendar.DAY_OF_MONTH));
    }
    
    void genSerialNum() {
        String serial = odao.generateSerialNumber();
        if (serial==null) {
            txtSerialNumber.setText("0000001");
        } else {
            int increment = Integer.parseInt(serial);
            increment = increment + 1;
            txtSerialNumber.setText("000000"+increment);
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtSerialNumber = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txtCodClie = new javax.swing.JTextField();
        txtCodProd = new javax.swing.JTextField();
        txtPrice = new javax.swing.JTextField();
        spnQty = new javax.swing.JSpinner();
        btnSearchClie = new javax.swing.JButton();
        btnSearchProd = new javax.swing.JButton();
        btnAddOrder = new javax.swing.JButton();
        txtDateOrder = new javax.swing.JTextField();
        txtClient = new javax.swing.JTextField();
        txtProduct = new javax.swing.JTextField();
        txtStock = new javax.swing.JTextField();
        txtUser = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbOrderItems = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        btnCancel = new javax.swing.JButton();
        btnGenerateSale = new javax.swing.JButton();
        txtTotal = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();

        setTitle("SYSTEM OF SALES");

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel1.setFont(new java.awt.Font("Ubuntu", 1, 17)); // NOI18N
        jLabel1.setText("POINT OF SALE");

        jLabel2.setFont(new java.awt.Font("Ubuntu", 2, 15)); // NOI18N
        jLabel2.setText("sale of technological products");

        jLabel3.setFont(new java.awt.Font("Ubuntu", 0, 13)); // NOI18N
        jLabel3.setText("PHONE:  999-347-011");

        jLabel4.setFont(new java.awt.Font("Ubuntu", 1, 13)); // NOI18N
        jLabel4.setText("SERIAL NUMBER:");

        txtSerialNumber.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N

        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/apple-app-store.png"))); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addGap(47, 47, 47)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtSerialNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtSerialNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel6)
                .addGap(0, 11, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel5.setFont(new java.awt.Font("Ubuntu", 1, 12)); // NOI18N
        jLabel5.setText("COD. CLIE:");

        jLabel7.setFont(new java.awt.Font("Ubuntu", 1, 12)); // NOI18N
        jLabel7.setText("COD. PROD:");

        jLabel8.setFont(new java.awt.Font("Ubuntu", 1, 12)); // NOI18N
        jLabel8.setText("PRICE:");

        jLabel9.setFont(new java.awt.Font("Ubuntu", 1, 12)); // NOI18N
        jLabel9.setText("QTY:");

        txtCodProd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCodProdActionPerformed(evt);
            }
        });

        btnSearchClie.setFont(new java.awt.Font("Ubuntu", 0, 13)); // NOI18N
        btnSearchClie.setText("SEARCH");
        btnSearchClie.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchClieActionPerformed(evt);
            }
        });

        btnSearchProd.setFont(new java.awt.Font("Ubuntu", 0, 13)); // NOI18N
        btnSearchProd.setText("SEARCH");
        btnSearchProd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchProdActionPerformed(evt);
            }
        });

        btnAddOrder.setFont(new java.awt.Font("Ubuntu", 0, 13)); // NOI18N
        btnAddOrder.setText("ADD");
        btnAddOrder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddOrderActionPerformed(evt);
            }
        });

        jLabel10.setFont(new java.awt.Font("Ubuntu", 1, 12)); // NOI18N
        jLabel10.setText("CLIENT:");

        jLabel11.setFont(new java.awt.Font("Ubuntu", 1, 12)); // NOI18N
        jLabel11.setText("PRODUCT:");

        jLabel12.setFont(new java.awt.Font("Ubuntu", 1, 12)); // NOI18N
        jLabel12.setText("STOCK:");

        jLabel13.setFont(new java.awt.Font("Ubuntu", 1, 12)); // NOI18N
        jLabel13.setText("USER:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel9))
                        .addGap(50, 50, 50)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(spnQty, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtPrice, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(150, 150, 150)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtUser)
                            .addComponent(txtStock)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel5))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtCodClie, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)
                            .addComponent(txtCodProd))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSearchClie)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel10)
                        .addGap(18, 18, 18)
                        .addComponent(txtClient))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(180, 180, 180)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(btnSearchProd)
                                    .addComponent(btnAddOrder)))
                            .addComponent(txtDateOrder, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel11)
                                .addGap(2, 2, 2)
                                .addComponent(txtProduct))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel13)
                                    .addComponent(jLabel12))
                                .addGap(0, 0, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtCodClie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSearchClie)
                    .addComponent(txtClient, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtCodProd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSearchProd)
                    .addComponent(txtProduct, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtStock, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel8)
                        .addComponent(txtPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnAddOrder)
                        .addComponent(jLabel12)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(spnQty, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtDateOrder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13))
                .addGap(0, 25, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        tbOrderItems.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "NRO", "COD", "PRODUCT", "QTY", "PRICE/UNIT", "SUBTOTAL"
            }
        ));
        jScrollPane1.setViewportView(tbOrderItems);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 474, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        btnCancel.setText("CANCEL");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        btnGenerateSale.setText("GENERATE SALE");
        btnGenerateSale.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGenerateSaleActionPerformed(evt);
            }
        });

        txtTotal.setFont(new java.awt.Font("Ubuntu", 1, 14)); // NOI18N

        jLabel14.setFont(new java.awt.Font("Ubuntu", 1, 12)); // NOI18N
        jLabel14.setText("TOTAL:");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(btnCancel)
                .addGap(18, 18, 18)
                .addComponent(btnGenerateSale)
                .addGap(33, 33, 33)
                .addComponent(jLabel14)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancel)
                    .addComponent(btnGenerateSale)
                    .addComponent(txtTotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel14))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
            .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSearchClieActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchClieActionPerformed
        searchClient();
    }//GEN-LAST:event_btnSearchClieActionPerformed

    private void btnSearchProdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchProdActionPerformed
        searchProduct();
    }//GEN-LAST:event_btnSearchProdActionPerformed

    private void txtCodProdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCodProdActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCodProdActionPerformed

    private void btnAddOrderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddOrderActionPerformed
        addProduct();
    }//GEN-LAST:event_btnAddOrderActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        cancelOrder();
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnGenerateSaleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGenerateSaleActionPerformed
        if (txtTotal.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "You must enter the data");
        } else {
            addOrder();
            addOrderItem();        
            updateStockProducts();
            
            JOptionPane.showMessageDialog(this, "Order saved successfully!");
            cancelOrder(); 
            genSerialNum();
        }        
       
    }//GEN-LAST:event_btnGenerateSaleActionPerformed

    
    void addProduct() {
        double subtotal;
        int item = 0;
        model = (DefaultTableModel) tbOrderItems.getModel();
        item = item+1;
        
        idp = Integer.parseInt(txtCodProd.getText());
        String name = txtProduct.getText();
        price = Double.parseDouble(txtPrice.getText());
        int stock = Integer.parseInt(txtStock.getText());
        qty = Integer.parseInt(spnQty.getValue().toString());
        
        subtotal = qty*price;
        ArrayList list = new ArrayList();
        if(stock>0) {
            list.add(item);
            list.add(idp);
            list.add(name);
            list.add(qty);
            list.add(price);
            list.add(subtotal);
            
            Object[] obj = new Object[6];
            obj[0] = list.get(0);
            obj[1] = list.get(1);
            obj[2] = list.get(2);
            obj[3] = list.get(3);
            obj[4] = list.get(4);
            obj[5] = list.get(5);
            
            model.addRow(obj);
            tbOrderItems.setModel(model);
            
            calculateTotal();
            
        }else {
            JOptionPane.showMessageDialog(this, "product stock not available");
        }
        
    }
    
    void calculateTotal() {
        totalPayment = 0;
        for (int i = 0; i < tbOrderItems.getRowCount(); i++) {
            qty = Integer.parseInt(tbOrderItems.getValueAt(i, 3).toString());
            price = Double.parseDouble(tbOrderItems.getValueAt(i, 4).toString());
            totalPayment = totalPayment + (qty*price);            
        }
        txtTotal.setText(""+totalPayment);
    }
    
    void searchProduct() {
        int id = Integer.parseInt(txtCodProd.getText());
        if (txtCodProd.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "You must enter customer code");
        }else {
            Product product = pdao.listID(id);
            if(product.getId() != 0) {
                txtProduct.setText(product.getName());
                txtPrice.setText(""+product.getPrice());
                txtStock.setText(""+product.getStock());
                
            }else {
                JOptionPane.showMessageDialog(this, "Unregistered product, do you want to register?");
                txtCodProd.requestFocus();
            }
        }
    }

    void searchClient() {
        int r=0;
        String codClient = txtCodClie.getText();
        if(txtCodClie.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "You must enter customer code");
        }else {
            client = cdao.listID(codClient);
            
            if(client.getDni() != null) {
                txtClient.setText(client.getDisplayName());
                txtCodProd.requestFocus();
            }else {
                r = JOptionPane.showConfirmDialog(this, "Unregistered customer, do you want to register?");
                
                if(r==0) {
                    ClientForm cf = new ClientForm();
                    Principal.winPrincipal.add(cf);
                    cf.setVisible(true);
                }
            }
        }
    }
    
    void addOrder() {
        User usr = udao.userByName(txtUser.getText());
        int idu = usr.getId();        
        int idc = client.getId();
        String serial = txtSerialNumber.getText();
        String date = txtDateOrder.getText();
        double total = totalPayment;
        String status = "ACTIVE";
        
        o.setIduser(idu);
        o.setIdClient(idc);
        o.setSerialNumber(serial);
        o.setDate(date);
        o.setTotal(total);
        o.setFlag(status);
        
        odao.addOrder(o);
    }
    
    void addOrderItem() {
        String lastID = odao.idOrder();
        int ido = Integer.parseInt(lastID);
        
        for (int i = 0; i < tbOrderItems.getRowCount(); i++) {
            idp = Integer.parseInt(tbOrderItems.getValueAt(i, 1).toString());
            qty = Integer.parseInt(tbOrderItems.getValueAt(i, 3).toString());
            double subTotal = Double.parseDouble(tbOrderItems.getValueAt(i, 4).toString());
            
            oi.setIdOrder(ido);
            oi.setIdProduct(idp);
            oi.setQty(qty);
            oi.setSubtotal(subTotal);
            
            odao.addOrderItem(oi);
        }
                
    }
    
    
    void updateStockProducts() {
        for (int i = 0; i < model.getRowCount(); i++) {
            idp = Integer.parseInt(tbOrderItems.getValueAt(i, 1).toString());
            qty = Integer.parseInt(tbOrderItems.getValueAt(i, 3).toString());
            p = pdao.listID(idp);
            int currentStock = p.getStock() - qty;
            pdao.updateStock(currentStock, idp);
        }
    }
    
    void cancelOrder() {
        clearForm();
        txtCodClie.setText("");
        txtCodProd.setText("");
        txtPrice.setText("");
        spnQty.setValue(1);
        txtDateOrder.setText("");
        txtClient.setText("");
        txtProduct.setText("");
        txtStock.setText("");
        txtTotal.setText(""); 
        txtCodClie.requestFocus();
    }
    
    void clearForm() {
        for (int i = 0; i < model.getRowCount(); i++) {
            model.removeRow(i);
            i = i - 1;
        }
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddOrder;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnGenerateSale;
    private javax.swing.JButton btnSearchClie;
    private javax.swing.JButton btnSearchProd;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSpinner spnQty;
    private javax.swing.JTable tbOrderItems;
    private javax.swing.JTextField txtClient;
    private javax.swing.JTextField txtCodClie;
    private javax.swing.JTextField txtCodProd;
    private javax.swing.JTextField txtDateOrder;
    private javax.swing.JTextField txtPrice;
    private javax.swing.JTextField txtProduct;
    private javax.swing.JTextField txtSerialNumber;
    private javax.swing.JTextField txtStock;
    private javax.swing.JTextField txtTotal;
    public static javax.swing.JTextField txtUser;
    // End of variables declaration//GEN-END:variables
}
